// pages/dish/dish.js
let thisa;
let od;  //菜谱列表对象
Page({
  /**
   * 页面的初始数据
   */
  data: {
    shopname:'',
    navbar: ['点菜', '购物车', '评论'],
    currentTab: 0,
    buycarShow:true,
    goodsList:{},        //商品列表
    dishnum:{}
  },

  initShoppingCar(flowid){
    let x = flowid;
    console.log(x);
    // console.log(flowid);
    let y = wx.getStorageSync('flowid');
    console.log(y=='undefined');
    console.log(x==y);
    if (wx.getStorageSync('flowid') != 'undefined' && wx.getStorageSync('flowid')!=flowid){
      wx.setStorageSync('shoppingcar', '{}');
    }else{
      wx.setStorageSync('flowid', flowid);
    }
    // console.log(wx.getStorageSync('shoppingcar'));
  },
  
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    thisa=this;
    let flowid = options.restaurantCode;
    od = this.selectComponent("#od");
    od.init(flowid);
    thisa.initShoppingCar(flowid);
  },
  navbarTap: function (e) {
    // let od = this.selectComponent("#od");
    // od.fun1();
    this.setData({
      currentTab: e.currentTarget.dataset.idx
    })
  },
  showBuyCar:function(e){
    let str = wx.getStorageSync('shoppingcar');
    let json = JSON.parse(str);
    
    // if (thisa.data.buycarShow){
      thisa.setData({
        buycarShow: false,
        goodsList:json
      });
    // }else{
    //   thisa.setData({
    //     buycarShow: true
    //   });
    // }
    
  },
  hidBuyCar:function(e){
    thisa.setData({
      buycarShow: true
    });
  },
  addInCar:function(e){
    let target = e.currentTarget;
    let dishid = e.currentTarget.id;
    console.log(dishid)
    console.log(od.data.dishnum)
    // let dname = target.dataset.dname;
    // let price = target.dataset.price;
    let dishnum = od.data.dishnum;
    dishnum[dishid]+=1;
    let jsonStr = wx.getStorageSync('shoppingcar');
    let json = JSON.parse(jsonStr);
    json[dishid][2] +=1 
    wx.setStorageSync('shoppingcar', JSON.stringify(json));
    thisa.showBuyCar();
    od.setData({
      dishnum: dishnum
    })
  },
  lessInCar: function (e) {
    let target = e.currentTarget;
    let dishid = e.currentTarget.id;
    console.log(dishid)
    console.log(od.data.dishnum)
    // let dname = target.dataset.dname;
    // let price = target.dataset.price;
    let dishnum = od.data.dishnum;
    dishnum[dishid] -= 1;
    let jsonStr = wx.getStorageSync('shoppingcar');
    let json = JSON.parse(jsonStr);
    json[dishid][2] -= 1;
    if(json[dishid][2]==0){
      delete json[dishid];
    }
    wx.setStorageSync('shoppingcar', JSON.stringify(json));
    thisa.showBuyCar();
    od.setData({
      dishnum: dishnum
    })
  },

  dian:function(e){

  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})